package com.ruoyi.produce.base.offer.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.produce.base.offer.domain.ProduceOfferCond;
import com.ruoyi.produce.base.offer.domain.ProduceOfferDomain;
import com.ruoyi.produce.base.offer.domain.ProduceOfferDto;
import com.ruoyi.produce.base.offer.service.IProduceOfferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 视图层
 * 生产报价
 */
@RestController
@RequestMapping("/produce/offer")
@Api(value = "生产报价信息模块", tags = "生产报价接口")
public class ProduceOfferController extends BaseController {
    @Autowired
    private IProduceOfferService produceOfferService;

    /**
     * 获取生产报价列表
     * @return
     */
    @ApiOperationSupport(includeParameters = {"ProduceOfferCond.produceOfferId", "ProduceOfferCond.produceId", "ProduceOfferCond.createTime"})
    @ApiOperation(value = "获取生产报价列表",notes = "查询所有生产报价信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProduceOfferCond cond) {
        startPage();
        List<ProduceOfferDto> produceOfferDtos = produceOfferService.selectProduceOfferList(cond);
        return getDataTable(produceOfferDtos);
    }

    /**
     * 新增生产报价
     *
     * @param produceOffer
     * @return
     */
    @ApiOperation(value = "新增生产报价记录", notes = "新增生产报价信息")
    @ApiImplicitParam(name = "produceOffer", value = "新增生产报价", dataType = "ProduceOfferDomain")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProduceOfferDomain produceOffer) {
        if (produceOfferService.selectProduceOfferByPlanSelfId(produceOffer.getProduceOfferId())) {
            return AjaxResult.error("该生产计划已有报价，无法重复添加");
        }
        return toAjax(produceOfferService.insertProduceOffer(produceOffer));
    }

    /**
     * 修改生产报价记录
     *
     * @param produceOffer
     * @return
     */
    @ApiOperation(value = "修改生产报价", notes = "修改生产报价信息")
    @ApiImplicitParam(name = "produceOffer", value = "修改生产报价", dataType = "ProduceOfferDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProduceOfferDomain produceOffer) {
        try {
            produceOfferService.updateProduceOffer(produceOffer);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除生产报价记录
     * 仅修改删除标志位
     *
     * @param produceOffer
     * @return
     */
    @ApiOperation(value = "删除生产报价", notes = "删除生产报价,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ProduceOfferDomain produceOffer) {
        return toAjax(produceOfferService.updateProduceOfferDeleteFlag(produceOffer));
    }
}
